<?php
/*
 * @Author: 李明
 * @Date: 2021-12-15 17:35:54
 * @LastEditTime: 2022-01-21 15:52:07
 * @LastEditors: 李明
 * @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Resources/Order/OrderDetail.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Resources\Order;

use App\Http\Controllers\common\LMReturn;
use App\Http\Resources\Image;
use App\Models\TlBankCompany;
use Illuminate\Http\Resources\Json\JsonResource;

class OrderDetail extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $order = parent::toArray($request);
        $pro_name = $this->user->pro->area_name ?? '';
        $a_area_name = $pro_name;
        $city_name = $this->user->city->area_name ?? '';
        $a_area_name = $a_area_name == '' ? $city_name : $a_area_name . '/' . $city_name;
        $area_name = $this->user->area->area_name ?? '';
        $a_area_name = $a_area_name == '' ? $area_name : $a_area_name . '/' . $area_name;
        $order['user']['area_name'] = $pro_name . '/' . $city_name . '/' . $area_name;
        $order['to_account'] = $this->working_family->open_bank ?? '未知';
        $order['families_money_records'] = $this->families_money_records->where('type', '1');
        $product = $this->product;
        $time_unit = $product->time_unit;
        $long_time = $product->long_time;
        $pay_long = $product->pay_long;
        $pay_time_unit = $product->pay_time_unit;
        $order['results_money'] = (int)($this->results_money);
        $order['valid_time'] = date('Y-m-d', strtotime($this->valid_time));
        $order['sign_time'] = date('Y-m-d', strtotime($this->sign_time));
        $order['account_time'] = date('Y-m-d', strtotime($this->account_time));
        // dd($order['sign_time']);
        $order['contract_end_date'] =  date('Y-m-d', strtotime($this->contract_end_date));
        // pay_images 支付凭证  -1未知 1身份证正2身份证反3转款凭证4合同照片   5: '银行卡正面',6: '银行卡反面',7: '其他'
        // print($order->images->where('img_type', '7')->all());
        // img_type 支付凭证  -1未知 1身份证正2身份证反3转款凭证4合同照片   5: '银行卡正面',6: '银行卡反面',7: '其他'
        // $order['other_images'] = Image::collection($order->images->where('img_type', '7')->all());
        $order['other_images'] = Image::collection($this->images->where('img_type', '7')->all());
        $order['ht_images'] = Image::collection($this->images->where('img_type', '4')->all());
        $order['pay_images'] = Image::collection($this->images->where('img_type', '3')->all());
        $order['images']  = Image::collection($this->images);
        $interest_records = json_decode($this->interest_records, true);
        if (count($interest_records) > 0) {
            foreach ($interest_records as $key => $value) {
                $interest_records[$key]['date'] = date('Y-m-d', strtotime($value['date']));
                if ($value['real_time'] != null) {
                    $interest_records[$key]['real_time'] = date('Y-m-d', strtotime($value['real_time']));
                }
            }
        }
        $order['interest_records'] = $interest_records;
        switch ($this->investment_way) { // 投资状态  1常规  2通联付款
            case '1':
                $order['pay_type'] = '常规';
                break;
            case '2':
                $order['pay_type'] = '待付款';
                break;
            case '3':
                $order['pay_type'] = '已付款';
                break;
            default:
                $order['pay_type'] = '未知';
                break;
        }
        if (($this->investment_way == 2 || $this->investment_way == 2) && ($order['order_status'] == '-1' || $order['order_status'] == '1' || $order['order_status'] == '2')) {
            $where = [];
            $where[] = ['type', 1];
            $order['tl_account_list'] = TlBankCompany::where($where)->get();
        } else {
            $order['tl_account_list'] = [];
        }
        switch ($time_unit) { // 周期单位   1日、 2月、3年
            case '1':
                $order['product']['product_longtime'] = $long_time . '日';
                break;
            case '2':
                $order['product']['product_longtime'] = $long_time . '月';
                break;
            case '3':
                $order['product']['product_longtime'] = $long_time . '年';
                break;
            default:
                $order['product']['product_longtime'] = '';
                break;
        }
        switch ($pay_time_unit) {
            case '1':
                $order['product']['product_pay_long'] = $pay_long . '日/次';
                break;
            case '2':
                $order['product']['product_pay_long'] = $pay_long . '月/次';
                break;
            case '3':
                $order['product']['product_pay_long'] = $pay_long . '年/次';
                break;
            default:
                $order['product']['product_pay_long'] = '';
                break;
        }
        switch ($this->is_first) { // 是否为首个订单  1是新单2否存量 3转续
            case '1':
                $order['order_type'] = '新单';
                break;
            case '2':
                $order['order_type'] = '存量';
                break;
            case '3':
                $order['order_type'] = '转续';
                break;
            default:
                $order['order_type'] = '数据异常';
                break;
        }
        switch ($this->order_status) { // 订单状态 -1草稿， 1是待付款  2是审核中   3是待生效(弃用)  4是已生效 5是拒绝  6是已结束
            case '-1':
                $order['order_status_str'] = '草稿';
                break;
            case '1':
                $order['order_status_str'] = '待付款';
                break;
            case '2':
                switch ($this->audit_step) { // 是否为首个订单  1初审 2复审
                    case '1':
                        $order['order_status_str'] = '初审中';
                        break;
                    case '2':
                        $order['order_status_str'] = '复审中';
                        break;
                }
                break;
            case '3':
                $order['order_status_str'] = '待生效';
                break;
            case '4':
                $order['order_status_str'] = '已生效';
                break;
            case '5':
                $order['order_status_str'] = '拒绝';
                break;
            case '6':
                $order['order_status_str'] = '已完结';
                break;
            default:
                $order['order_status_str'] = '数据异常';
                break;
        }
        $fdd_records = $this->fdd_records;
        $user_status = 0;
        $institution_status = 0;
        $guarantee_status = 0;
        foreach ($fdd_records as $record) {
            if ($record['status'] == 1) {
                if ($record['user_type'] == 1) {
                    $user_status = 1;
                } elseif ($record['user_type'] == 2) {
                    $institution_status = 1;
                } elseif ($record['user_type'] == 3) {
                    $guarantee_status = 1;
                }
            }
        }
        $order['user_status'] = $user_status;
        $order['institution_status'] = $institution_status;
        $order['guarantee_status'] = $guarantee_status;
        return $order;
    }
}
